﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TraCuuDiemNhom3
{
    public partial class NhapDiem : System.Web.UI.Page
    {
        DataTable tblDiem,tblLop, tblMonHoc;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //tblDiem = TienIchSQL.ChayLenhSelectDuLieu("Select * from Diem");
                //gvDiem.DataSource = tblDiem;
                //gvDiem.DataBind();

                tblLop = TienIchSQL.ChayLenhSelectDuLieu("select  distinct  substring([UserName],1,6) as MaLop from NguoiDung where SubString(UserName,1,1)<>'E'");
                ddlLop.DataSource = tblLop;
                ddlLop.DataValueField = "MaLop";
                ddlLop.DataTextField = "MaLop";
                ddlLop.DataBind();

                //tblMonHoc = TienIchSQL.ChayLenhSelectDuLieu("Select * from MonHoc");
                //ddlMonHoc.DataSource = tblMonHoc;
                //ddlMonHoc.DataTextField = "TenMH";
                //ddlMonHoc.DataValueField = "MaMH";
                //ddlMonHoc.DataBind();

                ddlHocKy_SelectedIndexChanged( sender,  e);
               // tblMonHoc = TienIchSQL.ChayLenhSelectDuLieu("Select distinct * from MonHoc");//, nguoidung n where m.MaMH=d.MaMH and d.MaSV = n.UserName and SubString(n.UserName,5,2)=@maLop and d.HocKy=@hocKy", "@maLop", ddlLop.SelectedValue, "@hocKy", ddlHocKy.Text);
                //ddlMonHoc.DataSource = tblMonHoc;
                //ddlMonHoc.DataTextField = "TenMH";
                //ddlMonHoc.DataValueField = "MaMH";
                //ddlMonHoc.DataBind();

            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            
//--Update Diem
//--set KT1='7', KT2= '7' , KT3= '7' , KT4= '7' , KT5= '7' , THI1= '8' , TK= '7.2' 
//--where MaMH='MH03' and MaSV='0004' and HocKy=2
            
            for (int i = 0; i < gvDiem.Rows.Count; i++)
            {
                GridViewRow row = gvDiem.Rows[i];
                if (row.RowType != DataControlRowType.DataRow)
                    continue;
                string MaSV = ((HiddenField)row.FindControl("hidenMaSV")).Value;
                string MaMH= ddlMonHoc.SelectedValue;
                int HocKy= int.Parse(ddlHocKy.Text);

                object kt1, kt2, kt3, kt4, kt5,thi1, tk;
                TextBox txtKt1 = (TextBox)row.FindControl("txtKt1");
                if (string.IsNullOrEmpty(txtKt1.Text))
                    kt1 = 0;
                else
                    kt1 = float.Parse(txtKt1.Text);

                TextBox txtKt2 = (TextBox)row.FindControl("txtKt2");
                if (string.IsNullOrEmpty(txtKt2.Text))
                    kt2 = 0;
                else
                    kt2 = float.Parse(txtKt2.Text);

                TextBox txtKt3 = (TextBox)row.FindControl("txtKt3");
                if (string.IsNullOrEmpty(txtKt3.Text))
                    kt3 = 0;
                else
                    kt3 = float.Parse(txtKt3.Text);

                TextBox txtKt4 = (TextBox)row.FindControl("txtKt4");
                if (string.IsNullOrEmpty(txtKt4.Text))
                    kt4 = 0;
                else
                    kt4 = float.Parse(txtKt4.Text);

                TextBox txtKt5 = (TextBox)row.FindControl("txtKt5");
                if (string.IsNullOrEmpty(txtKt5.Text))
                    kt5 = 0;
                else
                    kt5 = float.Parse(txtKt5.Text);

                TextBox txtThi = (TextBox)row.FindControl("txtThi");
                if (string.IsNullOrEmpty(txtKt2.Text))
                    thi1 = 0;
                else
                    thi1 = float.Parse(txtKt2.Text);

                TextBox txtTk = (TextBox)row.FindControl("txtTk");
                if (string.IsNullOrEmpty(txtTk.Text))
                    tk = 0;
                else
                    tk = float.Parse(txtTk.Text);
             
                TienIchSQL.ChayLenhUpdateDuLieu("Update Diem set KT1=@KT1, KT2=@KT2 , KT3=@KT3 , KT4= @KT4 , KT5= @KT5 , THI1= @THI1, TK= @TK where  MaSV=@maSV  and HocKy= @hocKy and MaMH=@maMH", "@KT1", kt1, "@KT2", kt2, "@KT3", kt3, "@KT4", kt4, "@KT5", kt5, "@THI1", thi1, "@TK", tk, "@MaSV", MaSV, "@hocky", HocKy, "@maMH", MaMH);

                  }
           }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void gvDiem_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void lbLop_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            tblDiem = TienIchSQL.ChayLenhSelectDuLieu("Select*, n.HoTen From Diem d, NguoiDung n  where d.MaSV=n.UserName and SubString(n.UserName,1,6) = @maLop and d.HocKy= @hocKy and d.MaMH=@maMH", "@maLop", ddlLop.Text, "@hocKy", int.Parse(ddlHocKy.SelectedValue), "@maMH", ddlMonHoc.SelectedValue);                                            
            gvDiem.DataSource = tblDiem;
            gvDiem.DataBind();        
        }

        protected void ddlHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            tblMonHoc = TienIchSQL.ChayLenhSelectDuLieu("Select distinct  m.MaMH, m.TenMH from MonHoc m, diem d, nguoidung n where m.MaMH=d.MaMH and d.MaSV = n.UserName and SubString(n.UserName,5,2)=@maLop and d.HocKy=@hocKy", "@maLop", ddlLop.SelectedValue, "@hocKy", ddlHocKy.Text);
            ddlMonHoc.DataSource = tblMonHoc;
            ddlMonHoc.DataTextField = "TenMH";
            ddlMonHoc.DataValueField = "MaMH";
            ddlMonHoc.DataBind();
        }

        protected void ddlMonHoc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlHocKy_SelectedIndexChanged(sender, e);
            //tblMonHoc = TienIchSQL.ChayLenhSelectDuLieu("Select  distinct m.MaMH, m.TenMH from MonHoc m, diem d, nguoidung n where m.MaMH=d.MaMH and d.MaSV = n.UserName and SubString(n.UserName,5,2)=@maLop and d.HocKy=@hocKy", "@maLop", ddlLop.SelectedValue, "@hocKy", ddlHocKy.Text);
            //ddlMonHoc.DataSource = tblMonHoc;
            //ddlMonHoc.DataTextField = "TenMH";
            //ddlMonHoc.DataValueField = "MaMH";
            //ddlMonHoc.DataBind();
        }
    }
}